//
//  RWHomePageCollectionViewCell.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/26.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWHomePageCollectionViewCell.h"
#import "RWGoodsModel.h"

@interface RWHomePageCollectionViewCell ()

@property (nonatomic, copy) RWGoodsModel *goodsModel;

@end

@implementation RWHomePageCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self setHomePageCell];
    }
    
    return self;
}

#pragma mark - Load Data

- (void)loadDataWithModel:(id)model{

    _goodsModel = [RWGoodsModel mj_objectWithKeyValues:model];
    [self configDatas];
}

- (void)configDatas{
    
    [_imageV sd_setImageWithURL:[NSURL URLWithString:_goodsModel.goods_image] placeholderImage:[UIImage imageNamed:@"huibangke"]];
    _originalMoneyLab.text = [NSString stringWithFormat:@"市场价：¥ %@", _goodsModel.goods_marketprice];
    _moneyLab.text = [NSString stringWithFormat:@"尊享价¥：%@", _goodsModel.goods_price];
    _titleLab.text = _goodsModel.goods_name;
    
    //收藏按钮
    if (_goodsModel.is_coll == 1) {
        _collectionIcon.selected = YES;
    } else {
        _collectionIcon.selected = NO;
    }
}

#pragma mark - Config UI

- (void)setHomePageCell{

    _imageV = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageV];
    
    _titleLab = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLab];
    _titleLab.font = FTY_FONTTSIZE(14);
    [_titleLab sizeToFit];
    _titleLab.numberOfLines = 2;
    
    _originalMoneyLab = [[UILabel alloc] init];
    [self.contentView addSubview:_originalMoneyLab];
    _originalMoneyLab.textColor = [UIColor blackColor];
    _originalMoneyLab.font = FTY_FONTTSIZE(13);
    
    _moneyLab = [[UILabel alloc] init];
    [self.contentView addSubview:_moneyLab];
    _moneyLab.textColor = [FTYColor getMainColorWithAlpha:1.];
    _moneyLab.font = FTY_FONTTSIZE(13);
    
    _cartIcon = [[UIButton alloc] init];
    [self.contentView addSubview:_cartIcon];
    [_cartIcon setImage:[UIImage imageNamed:@"shop_icon"] forState:UIControlStateNormal];
    [_cartIcon setImage:[UIImage imageNamed:@"shop_sel_icon"] forState:UIControlStateSelected];
    [_cartIcon addTarget:self action:@selector(cartIconAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _collectionIcon = [[UIButton alloc] init];
    [self.contentView addSubview:_collectionIcon];
    [_collectionIcon setImage:[UIImage imageNamed:@"focus_icon"] forState:UIControlStateNormal];
    [_collectionIcon setImage:[UIImage imageNamed:@"focus_sel_icon"] forState:UIControlStateSelected];
    [_collectionIcon addTarget:self action:@selector(collectionIconAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //竖线（右侧）
    UIView *line1 = [[UIView alloc] init];
    [self.contentView addSubview:line1];
    line1.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.width.mas_equalTo(.5*FTY_WIDTH_BASE);
    }];
    
    //横线（底部）
    UIView *line2 = [[UIView alloc] init];
    [self.contentView addSubview:line2];
    line2.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
}

#pragma mark - Private Invoke Methods

- (void)collectionIconAction:(UIButton *)btn{
    
    if (_delegate && [_delegate respondsToSelector:@selector(collection:atIndex:)]) {
        [_delegate collection:btn atIndex:self.tag];
    }
}

- (void)cartIconAction:(UIButton *)btn{
    
    if (_delegate && [_delegate respondsToSelector:@selector(shoppingCart:atIndex:)]) {
        [_delegate shoppingCart:btn atIndex:self.tag];
    }
}

#pragma mark - Layout

- (void)layoutSubviews{

    [_imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(240.*FTY_HEIGHT_BASE);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageV.mas_bottom).offset(8.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
    }];
    
    [_originalMoneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom).offset(8.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
    }];
    
    [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_originalMoneyLab.mas_bottom).offset(8.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(_originalMoneyLab.mas_left);
    }];
    
    [_cartIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom).offset(8.*FTY_HEIGHT_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
        make.width.height.mas_equalTo(25.*FTY_WIDTH_BASE);
    }];
    
    [_collectionIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom).offset(8.*FTY_HEIGHT_BASE);
        make.right.mas_equalTo(_cartIcon.mas_left).offset(-8.*FTY_WIDTH_BASE);
        make.width.height.mas_equalTo(25.*FTY_WIDTH_BASE);
    }];
}

@end
